<template>
  <div>
    <el-card>
      <template #header>
        <el-row type="flex" justify="space-between">
          <el-col>

            <el-button v-if="checkPermBtn('addBtn')" size="small" type="primary" @click="addcategories">添加分类</el-button>
          </el-col>

          <el-col>
            <el-input v-model="title_contains" placeholder="请输入内容" class="input-with-select">
              <el-button slot="append" icon="el-icon-search" @click="searchFn" />
            </el-input>
          </el-col>
        </el-row>
      </template>
      <!-- 上方按钮- 搜索栏 -->

      <el-table :data="categoriesListTable" row-key="id">
        <el-table-column label="图片" prop="image">
          <template v-slot="{row}">
            <img
              v-imgErr="require('@/assets/common/bigUserHeader.png')"
              :src="row.image"
              style="width:160px"
            >

          </template>
        </el-table-column>
        <el-table-column label="标题" prop="title" />
        <el-table-column label="介绍" prop="desc" />
        <el-table-column label="操作">
          <template v-slot="{row}">
            <el-button v-if="checkPermBtn('editBtn')" type="primary" size="small" @click="editFn(row.id)">编辑</el-button>
            <el-button v-if="checkPermBtn('delBtn')" type="danger" size="small" @click="delFn(row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <!-- 分页器 -->
      <el-row type="flex" justify="center">
        <el-pagination
          layout="prev, pager, next"
          :page-size="limit"
          :total="total"
          @current-change="currentChange"
        />
      </el-row>
    </el-card>
    <Addcategories
      ref="Editcategories"
      :show-dialog.sync="visibleDialog"
      @closeEvent="visibleDialog=false"
      @updateEvent="getCategoriesList"
    />
  </div>
</template>

<script>
import { categoriesList, categoriesNumber, DeleteCategoires, SerchCategories } from '@/api/categories'
import Addcategories from './components/Addcategories.vue'
export default {
  components: { Addcategories },
  data() {
    return {
      visibleDialog: false,
      categoriesListTable: [],
      limit: 5,
      start: 0,
      total: 0,
      title_contains: ''
      // page: 0
    }
  },
  created() {
    this.getCategoriesList()
    this.getcategoriesNumber()
  },
  methods: {
    // 添加分类 按钮
    addcategories() {
      this.visibleDialog = true
      this.getCategoriesList()
    },
    // 编辑
    editFn(id) {
      this.visibleDialog = true
      // 数据回显
      this.$refs.Editcategories.getCategoriesById(id)
    },

    // --------------------------------------------------------------
    //  模糊搜索
    async searchFn(title_contains) {
      const res = await SerchCategories({
        _limit: this.limit,
        // _start: this.start,
        title_contains: this.title_contains
      })
      // console.log(res)
      this.categoriesListTable = res.data
      this.getcategoriesNumber()
    },

    // 删除 ------没有刷新页面
    async  delFn(id) {
      this.$confirm('是否确定删除？', '提示', {
        type: 'warning'
      }).then(async() => {
        // 调用接口
        await DeleteCategoires(id)

        if (this.categoriesListTable.length === 1 && this.start !== 1) {
          this.start -= 2
        }
        this.getCategoriesList()
      }).catch((err) => {
        console.log('点击了取消', err)
      })
    },

    /* ------------------------------------------------------------------------- */

    // 获取数据，渲染页面
    async getCategoriesList() {
      const res = await categoriesList({
        _limit: this.limit,
        _start: this.start
      })
      this.categoriesListTable = res.data
      this.getcategoriesNumber()
    },
    // 获取分类列表数量
    async getcategoriesNumber() {
      const res = await categoriesNumber({ title_contains: this.title_contains })
      this.total = res.data
      console.log('total', this.total)
    },
    // 分页  页码发生改变时触发
    currentChange(page) {
      this.start = (page - 1) * this.limit
      this.getCategoriesList()
      // this.getcategoriesNumber()
    }
  }

}
</script>

<style>

</style>
